//#include "stdafx.h"
#include <string>
#include <fstream>
#pragma warning(push)
#pragma warning(disable: 6011)
#include <boost/algorithm/string.hpp>
#pragma warning(pop)

//#include "utility_new.h"
#include "file/file.h"
using namespace std;
using namespace boost;

namespace KCore
{

	void string_replace(std::string& strBig, const std::string& strsrc, const std::string& strdst) 
	{
		replace_all(strBig,strsrc,strdst);
	}

	bool string_writetofile(const char *file,const std::string& data)
	{
		std::ofstream ofs(file,ios::binary);
		if (!ofs.is_open())
			return false;
		return (ofs << data) != 0;
	}

	bool string_writetofile(const char *file,const void* buf,size_t size)
	{
		std::ofstream ofs(file,ios::binary);
		if (!ofs.is_open())
			return false;
		return ofs.write((const char*)buf,size) != 0;
	}

	bool string_readfromfile(std::ifstream& ifs,void* buf,size_t size)
	{
		if (!ifs.is_open())
			return false;
		return ifs.read((char*)buf,size) != 0;
	}

	bool string_readfromfile(std::ifstream& ifs,std::string& data)
	{
		if (!ifs.is_open())
			return false;
		data = std::string((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
		return true;
	}

	bool string_readfromfile(const char *file,std::string& data)
	{
		std::ifstream ifs(file,ios::binary);
		return string_readfromfile(ifs,data);
	}

	bool string_readfromfile(const char *file,void* buf,size_t size)
	{
		std::ifstream ifs(file,ios::binary);
		return string_readfromfile(ifs,buf,size);
	}

	void string_lower(std::string& s)
	{
		to_lower(s);
	}

	void string_trim(std::string& s)
	{
		trim(s);
	}


	void help_file_nameformat(std::string& file)
	{
		string_lower(file);
		string_replace(file,"/","\\");
	}

	bool string_endis(const std::string& s,const std::string& end)
	{
		return iends_with(s,end);
	}

	bool is_string_like(const std::string& s, const std::string& des)
	{
		std::string::size_type pos = des.find("*");

		std::string s1 = des.substr(0,pos);
		std::string s2 = des.substr(pos+1,des.length());

		if (s.find(s1,0) == 0)
		{
			if (s.find(s2,s1.length())== s.length()-s2.length())
			{
				return true;
			}
		}
		return false;
	}

};
